<template>
  <div>
    <el-breadcrumb class="breadcrumb-row" separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>广告管理</el-breadcrumb-item>
      <el-breadcrumb-item>维护广告位置</el-breadcrumb-item>
    </el-breadcrumb>

    <el-card>
      <div style="position: relative;margin-left:50%;transform:translateX(-50%)">
        <el-form ref="form" :model="form" label-width="80px" :rules="rules">
          <el-form-item label="名称：" prop="title">
            <el-input v-model="form.title" placeholder="请输入名称" size="small"></el-input>
          </el-form-item>

          <el-form-item>
            <el-button type="primary" @click="submitForm" size="small">保存</el-button>
            <el-button @click="pageBack" size="small">返回</el-button>
          </el-form-item>
        </el-form>
      </div>
    </el-card>
  </div>
</template>

<script>
import {adPosition, status} from "../../request";

export default {
  name: "Save",
  data() {
    return {
      form: {title: ''}, rules: {
        title: [{required: true, message: '*必填'}]
      }
    }
  },
  created() {
    if (this.$route.params.id) {
      this.getInfo(this.$route.params.id);
    }
  },
  methods: {
    async getInfo(id) {
      const res = await adPosition.read({id});
      if (res.code == status.SUCCESS) {
        this.form.id = res.data.id;
        this.form.title = res.data.title;
      }
    },
    pageBack() {
      this.$router.back();
    },
    submitForm() {
      this.$refs.form.validate(async valid => {
        if (!valid) return this.$message.error("请完善有效的信息！");

        const res = await adPosition.add(this.form);
        if (res.code == status.SUCCESS) {
          this.$message.success(res.msg);
          setTimeout(() => {
            this.$router.back();
          }, 2000);
        } else this.$message.error(res.msg);
      });
    },
  }
}
</script>

<style scoped>

</style>
